﻿using System;
using System.Collections.Generic;
using GameServer.Script.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Service;
using ZyGames.Framework.Game.Sns;
using ZyGames.Framework.Game.Runtime;
using ZyGames.Framework.Game.Lang;

namespace GameServer.Script.CsScript.Action
{

    public class Action1004 : LoginExtendAction
    {
        /// <summary>
        /// 授权凭证
        /// </summary>
        private string _token;

        /// <summary>
        /// 末填写则默认为1.0版本
        /// </summary>
        //private string _clientAppVersion = "1.0";

        public Action1004(ActionGetter actionGetter)
            : base((short)1004, actionGetter)
        {

        }

        protected override bool DoSuccess(int userId, out IUser user)
        {
            user = null;
            var cacheSet = new PersonalCacheStruct<GameUser>();
            var roleCache = new PersonalCacheStruct<UserRole>();
            var roleList = roleCache.FindAll(userId.ToString());
            GameUser gameUser = cacheSet.FindKey(userId.ToString());
            if (gameUser == null || roleList.Count == 0)
            {
                //通知客户跳转到创建角色接口
                GuideId = 1005;
                return true;
            }
            user = new SessionUser(gameUser);
            if (gameUser.CurrRoleId == 0)
            {
                gameUser.CurrRoleId = roleList[0].RoleId;
            }

            ////添加排行
            //var cache = new ShareCacheStruct<UserRanking>();
            //var ranking = cache.Find(m => m.UserName == roleList[0].RoleName);
            //if (ranking == null)
            //{
            //    var userR = new GameUser() { UserId = gameUser.UserId, NickName = roleList[0].RoleName };
            //    new PersonalCacheStruct<GameUser>().Add(userR);
            //    ranking = new UserRanking();
            //    ranking.UserID = userR.UserId;
            //    ranking.UserName = roleList[0].RoleName;
            //    ranking.Score = new Random().Next(50);
            //    ranking.Items.Add(new Model.EquiAttrInfo { UserName = ranking.UserName, Score = ranking.Score });

            //    cache.Add(ranking);
            //}
            //else
            //{
            //    ranking.UserName = roleList[0].RoleName;
            //    ranking.Score = new Random().Next(50);
            //}

            return true;
        }

        public override bool GetUrlElement()
        {
            if (httpGet.GetString("Token", ref _token)
                    && httpGet.GetEnum("MobileType", ref MobileType))
            {


                //httpGet.GetString("IMEI", ref DeviceID);
                //httpGet.GetString("RetailID", ref RetailID);
                //httpGet.GetWord("ScreenX", ref ScreenX);
                //httpGet.GetWord("ScreenY", ref ScreenY);
                //httpGet.GetString("ClientAppVersion", ref _clientAppVersion);

                actionGetter.GetInt("GameType", ref GameType);
                actionGetter.GetInt("ServerID", ref ServerID);
                actionGetter.GetString("DeviceID", ref DeviceID);
                //actionGetter.GetByte("Sex", ref Sex);
                //actionGetter.GetString("NickName", ref NickName);
                //actionGetter.GetString("HeadID", ref HeadID);
                //actionGetter.GetWord("ScreenX", ref ScreenX);
                //actionGetter.GetWord("ScreenY", ref ScreenY);
                return true;
            }
            return false;

        }

        protected override ILogin CreateLogin()
        {
            //return new AccountServerRedisLogin("127.0.0.1:6379", 0, _token); //6.7.9.11版本优化支持连Redis较验检查
            return new AccountServerLogin(GameEnvironment.Setting.AccountServerUrl, _token, DeviceID);
        }

        protected override void DoLoginFail(ILogin login)
        {
            ErrorCode = Language.Instance.ErrorCode;
            ErrorInfo = Language.Instance.ServerBusy;
        }

        public async override void TakeActionAffter(bool state)
        {
            if (state)
            {
                Console.WriteLine("1004>发送World通知...");
                var notifyUsers = new List<IUser>();
                notifyUsers.Add(Current.User);
                await ActionFactory.SendAsyncAction(notifyUsers, (int)ActionType.World, null, t =>
                 {
                     Console.WriteLine("1004>发送World通知结果:{0}", t.Result.ToString());
                 });
            }
            base.TakeActionAffter(state);
        }
    }

    ///// <summary>
    ///// 1004_用户登录
    ///// </summary>
    //public class Action1004 : LoginExtendAction
    //{
    //    public Action1004(ActionGetter actionGetter)
    //        : base((short)ActionType.Login, actionGetter)
    //    {
    //    }

    //    protected override bool DoSuccess(int userId, out IUser user)
    //    {
    //        user = null;
    //        var cacheSet = new PersonalCacheStruct<GameUser>();
    //        var roleCache = new PersonalCacheStruct<UserRole>();
    //        var roleList = roleCache.FindAll(userId.ToString());
    //        GameUser gameUser = cacheSet.FindKey(userId.ToString());
    //        if (gameUser == null || roleList.Count == 0)
    //        {
    //            //通知客户跳转到创建角色接口
    //            GuideId = 1005;
    //            return true;
    //        }
    //        user = new SessionUser(gameUser);
    //        if (gameUser.CurrRoleId == 0)
    //        {
    //            gameUser.CurrRoleId = roleList[0].RoleId;
    //        }

    //        //添加排行
    //        var cache = new ShareCacheStruct<UserRanking>();
    //        var ranking = cache.Find(m => m.UserName == roleList[0].RoleName);
    //        if (ranking == null)
    //        {
    //            var userR = new GameUser() { UserId = gameUser.UserId, NickName = roleList[0].RoleName };
    //            new PersonalCacheStruct<GameUser>().Add(userR);
    //            ranking = new UserRanking();
    //            ranking.UserID = userR.UserId;
    //            ranking.UserName = roleList[0].RoleName;
    //            ranking.Score = new Random().Next(50);
    //            ranking.Items.Add(new Model.EquiAttrInfo { UserName = ranking.UserName, Score = ranking.Score });

    //            cache.Add(ranking);
    //        }
    //        else
    //        {
    //            ranking.UserName = roleList[0].RoleName;
    //            ranking.Score = new Random().Next(50);
    //        }

    //        return true;
    //    }

    //    public override void TakeActionAffter(bool state)
    //    {
    //        Console.WriteLine("1004>发送World通知...");
    //        var notifyUsers = new List<IUser>();
    //        notifyUsers.Add(Current.User);
    //        ActionFactory.SendAsyncAction(notifyUsers, (int)ActionType.World, null, t =>
    //        {
    //            Console.WriteLine("1004>发送World通知结果:{0}", t.Result.ToString());
    //        });
    //        base.TakeActionAffter(state);
    //    }
    //}
}
